//
//  SetEngineViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/1.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class SetEngineViewController: BaseViewController {
    
    
    let dataList:[NSViewController] = [
        SetBacisViewController(),
        SetUpdateViewController()
    ]
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_BasicWidth, kSet_BasicHeight))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    } 
    
    override func viewWillAppear() {
        super.viewWillAppear()
    }
    
    lazy var container: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        return view
    }()
    
    func addChildrens(vcs:[NSViewController],selectIndex:Int?)  {
        for (inx,val) in vcs.enumerated() {
            self.addChild(val)
            if inx == selectIndex {
                self.container.addSubview(val.view)
                val.view.snp.makeConstraints { (make) in
                    make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
                }
            }
        }
    }
    
    func setupUI() {
        self.view.addSubview(container)
        container.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addChildrens(vcs: dataList, selectIndex: 0)
        
    }
    
    func switchTo(vc:NSViewController) {
        //上一个高度
        let lastHeight = self.container.subviews[0].frame.size.height
        for sView in self.container.subviews {
            sView.removeFromSuperview()
        }
        
        
        self.container.addSubview(vc.view)
        vc.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(0, 0, 0, 0))
        }
        
        // 页面适配
        let x = self.view.window?.frame.origin.x ?? 0
        let y = self.view.window?.frame.origin.y ?? 0
        let ky = y+lastHeight-vc.view.frame.size.height
        self.view.window?.setContentSize(vc.view.frame.size)
        self.view.window?.setFrameOrigin(NSPoint.init(x: x, y:ky))
    }
    
    public func switchTo(index:Int) {
        if index > (self.dataList.count-1) {
            return
        }
        
        let viewController = self.dataList[index]
        self.switchTo(vc: viewController)
    }
}

